//
//  Seach_FiltrateAttribute_Tbv_WX.m
//  ZITHER
//
//  Created by weixin on 2019/3/25.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "Seach_FiltrateAttribute_Tbv_WX.h"
#import "Seach_FiltrateAttribute_Cell_WX.h"

@interface Seach_FiltrateAttribute_Tbv_WX ()

@property (nonatomic,strong)BaseBtn * priceBtn;
@property (nonatomic,strong)BaseBtn * priceTtitleBtn;

@end

@implementation Seach_FiltrateAttribute_Tbv_WX

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self def];
        [self setUI];
        [self setMasonry];
    }
    return self;
}

-(void)def{

    self.delegate = self;
    self.dataSource = self;
    
}

-(void)setUI{
    
}

-(void)setMasonry{
    
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"indexPath == %@",indexPath);
    NSLog(@"");
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    Seach_FiltrateAttribute_Model_WX *model = kUSER_Filtrate_MANAGER.arrAttribute[self.indexTbv];
    NSInteger index =  kUSER_Filtrate_MANAGER.arrAttribute[self.indexTbv].child.count;
    return  index>0?index-1:0;

//    NSLog(@"child = %ld",kUSER_Filtrate_MANAGER.arrAttribute[self.indexTbv].child.count);
//    return kUSER_Filtrate_MANAGER.arrAttribute[self.indexTbv].child.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSArray *arr = kUSER_Filtrate_MANAGER.arrAttribute[self.indexTbv].child[section].data;
    
    //0 1 2
    NSInteger index = arr.count/3+(arr.count%3==0?0:1);
    return index;
//    return index>0?index-1:index;
    
    if (arr.count!=0) {
        NSInteger index= arr.count/3;
        if (index == 0) {
            return 1;
        }
        if (index%3 ==0) {
            return index;
        }
        return index+1;
    }
    return 0;
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self getSeach_FiltrateAttribute_Cell_WX:indexPath];
    
}
-(UITableViewCell *)getSeach_FiltrateAttribute_Cell_WX:(NSIndexPath *)indexPath{
    static NSString *sectionCell = @"Seach_FiltrateAttribute_Cell_WX";
    Seach_FiltrateAttribute_Cell_WX *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[Seach_FiltrateAttribute_Cell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.indexPath = indexPath;
    cell.indexTbv = self.indexTbv;
    cell.BtnSelect = self.priceBtn.selected;
    [cell funcRefloadCell];
    
    //        cell.arr =
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    BaseView *view = [[BaseView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 30+20)];
    [self setHeaderView:view section:section];
//    [[view addTag]subscribeNext:^(id  _Nullable x) {
//        NSLog(@"123");
//        self.priceBtn.selected = !self.priceBtn.selected;
//        self.priceTtitleBtn.selected = !self.priceTtitleBtn.selected;
//        [self  reloadData];
//    }];
    return view;
}

-(void)setHeaderView:(UIView*)supview section:(NSInteger)section{
    
    BaseView *view = [[BaseView alloc]initWithFrame:CGRectMake(0, 20, 4, 16)];
    view.backgroundColor = baseColorTheme;
    [supview addSubview:view];
    
    
    QMUILabel *lab  =[[QMUILabel alloc]initWithFrame:CGRectMake(15, 20, SCREEN_WIDTH-40, 15)];
    lab.text =[NSString stringWithFormat:@"%@",kUSER_Filtrate_MANAGER.arrAttribute[self.indexTbv].child[section].groupName];
    [UIFont fontWithName:@"PingFangSC-Semibold" size:13];
//    lab.centerY = view.centerY;
    lab.textColor = [UIColor colorWithHex:@"333333"];
    if (section ==0 && self.indexTbv==1) {
        if (self.priceBtn.selected) {
            lab.text = kUSER_Filtrate_MANAGER.arrAttribute[self.indexTbv].child[0].groupName;
        }else{
            lab.text = kUSER_Filtrate_MANAGER.arrAttribute[self.indexTbv].child[1].groupName;
        }
    }else{
        lab.text = kUSER_Filtrate_MANAGER.arrAttribute[self.indexTbv].child[section+1].groupName;
    }
    [supview addSubview:lab];
    
    if (section ==0 && self.indexTbv==1) {
    self.priceBtn.hidden = NO;
    [supview addSubview:self.priceBtn];
    [supview addSubview:self.priceTtitleBtn];
        
        [self.priceTtitleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.priceBtn.mas_left).offset(-5);
            make.top.equalTo(supview).offset(19);
            make.height.mas_equalTo(15);
        }];
    }else{
//        self.priceBtn.hidden = YES;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat heightCell = 0;
    /* TODO: ================= cell 高度通过类型区分 =================*/
    
    heightCell = 44;
    
    return heightCell;
}

//设置区头不悬浮
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight = 50;
    
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0,0);
        
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
    
}


-(BaseBtn *)priceBtn
{
    if (!_priceBtn) {
        _priceBtn = [BaseBtn initCustomBtn:@[@"搜索_筛选_公价二手价",@"搜索_筛选_公价二手价"]];
        _priceBtn.frame = CGRectMake(SCREEN_WIDTH-15-15, 20, 15, 15);
//        _priceBtn.userInteractionEnabled = NO;
        @weakify(self);
        [[_priceBtn addTag]subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            self.priceBtn.selected = !self.priceBtn.selected;
            self.priceTtitleBtn.selected = !self.priceTtitleBtn.selected;

            [self  reloadData];
        }];
        
        
    }
    return _priceBtn;
}


-(BaseBtn *)priceTtitleBtn{
    if (!_priceTtitleBtn) {
        _priceTtitleBtn = [BaseBtn initCustomBtnTitlestype:@[@"公价筛选",@"二手价筛选"] color:@[baseColorTheme,baseColorTheme]];
        _priceTtitleBtn.frame = CGRectMake(SCREEN_WIDTH-15-15-100, 20, 100, 15);
        _priceTtitleBtn.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:13];
//        _priceTtitleBtn.userInteractionEnabled = NO;

        @weakify(self);
        [[_priceTtitleBtn addTag]subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            self.priceBtn.selected = !self.priceBtn.selected;
            self.priceTtitleBtn.selected = !self.priceTtitleBtn.selected;
            [self  reloadData];
        }];
    }
    return _priceTtitleBtn;
}

-(void)setIndexTbv:(NSInteger)indexTbv{
    _indexTbv = indexTbv;
    [self reloadData];
}

@end
